# -*- python -*-
# ex: set syntax=python:

# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import config
import master_site_config

from buildbot.changes.filter import ChangeFilter
from buildbot.process.properties import WithProperties
from buildbot.schedulers.basic import AnyBranchScheduler
from buildbot.schedulers.timed import Nightly
from buildbot.status import mail

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory
from master.factory import remote_run_factory

ActiveMaster = master_site_config.InfraCodesearch

c = BuildmasterConfig = {}
c['slavePortnum'] = ActiveMaster.slave_port
c['schedulers'] = []
c['builders'] = []
c['change_source'] = []
c['status'] = []

f_annotations = annotator_factory.AnnotatorFactory(ActiveMaster)

def m_remote_run(recipe, **kwargs):
  props = {'path_config': 'kitchen'}
  props.update(kwargs.pop('properties', {}))
  return remote_run_factory.RemoteRunFactory(
    active_master=ActiveMaster,
    repository=kwargs.pop(
      'repository', 'https://chromium.googlesource.com/infra/infra.git'),
    recipe=recipe,
    factory_properties=props,
    **kwargs)

revision_getter = master_utils.ConditionalProperty(
    lambda build: build.getProperty('revision'),
    WithProperties('%(revision)s'),
    'origin/master')

####### DATABASE

config.DatabaseSetup(c)

####### CHANGE SOURCES

INFRA_REPO_URL = 'https://chromium.googlesource.com/infra/infra'
TOOLS_BUILD_URL = 'https://chromium.googlesource.com/chromium/tools/build'
CHROMIUM_REPO_URL = 'https://chromium.googlesource.com/chromium/src'
GENERATED_FILES_REPO_URL = 'https://chromium.googlesource.com/chromium/src/out'
WEBRTC_REPO_URL = 'https://chromium.googlesource.com/external/webrtc'

c['change_source'].extend([
  gitiles_poller.GitilesPoller(INFRA_REPO_URL),
  gitiles_poller.GitilesPoller(CHROMIUM_REPO_URL),
  gitiles_poller.GitilesPoller(TOOLS_BUILD_URL),
  gitiles_poller.GitilesPoller(WEBRTC_REPO_URL),
])

####### BUILDERS

# TODO(emso): Move configs to recipes to avoid master restarts.

c['builders'].extend([
  {
    'name': 'codesearch-gen-chromium-android',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': 'codesearch-gen',
  },
  {
    'name': 'codesearch-gen-chromium-chromiumos',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': 'codesearch-gen',
  },
  {
    'name': 'codesearch-gen-chromium-linux',
    'factory': f_annotations.BaseFactory(recipe='chromium_codesearch'),
    'category': 'codesearch-gen',
  },
  {
    'name': 'codesearch-submodules-build',
    'factory': m_remote_run(
      'sync_submodules', repository=INFRA_REPO_URL,
      properties={'source_repo': TOOLS_BUILD_URL}),
    'category': 'codesearch-submodules',
  },
  {
    'name': 'codesearch-submodules-chromium',
    'factory': m_remote_run(
      'sync_submodules', repository=INFRA_REPO_URL,
      properties={
        'source_repo': CHROMIUM_REPO_URL,
        'extra_submodules': 'src/out=%s' % GENERATED_FILES_REPO_URL,
      }),
    'category': 'codesearch-submodules',
  },
  {
    'name': 'codesearch-submodules-infra',
    'factory': m_remote_run(
      'sync_submodules', repository=INFRA_REPO_URL,
      properties={'source_repo': INFRA_REPO_URL}),
    'category': 'codesearch-submodules',
  },
  {
    'name': 'codesearch-submodules-webrtc',
    'factory': m_remote_run(
      'sync_submodules', repository=INFRA_REPO_URL,
      properties={
        'source_repo': WEBRTC_REPO_URL,
	'dest_repo': WEBRTC_REPO_URL + '/codesearch/deps',
        'deps_path_prefix': 'src/',
      }),
    'category': 'codesearch-submodules',
  },

])

####### SCHEDULERS

c['schedulers'].extend([
  AnyBranchScheduler(
      name='build-scheduler',
      change_filter=ChangeFilter(project=['build']),
      treeStableTimer=1,
      builderNames=[
        'codesearch-submodules-build',
      ]
  ),
  AnyBranchScheduler(
      name='chromium-scheduler',
      change_filter=ChangeFilter(project=['src']),
      treeStableTimer=1,
      builderNames=[
        'codesearch-submodules-chromium',
      ]
  ),
  AnyBranchScheduler(
      name='infra-scheduler',
      change_filter=ChangeFilter(project=['infra']),
      treeStableTimer=30,
      builderNames=[
          'codesearch-submodules-infra',
      ]
  ),
  AnyBranchScheduler(
      name='webrtc-scheduler',
      change_filter=ChangeFilter(project=['webrtc']),
      treeStableTimer=30,
      builderNames=[
          'codesearch-submodules-webrtc',
      ]
  ),
  Nightly(
     name='gen-scheduler',
     branch='master',
     hour=range(0, 24, 4),
     minute=0,
     builderNames=[
       'codesearch-gen-chromium-android',
       'codesearch-gen-chromium-chromiumos',
       'codesearch-gen-chromium-linux',
     ]
  ),
])

####### BUILDSLAVES

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'InfraCodesearch')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(
    c['builders'], config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

####### STATUS TARGETS

# Adds common status and tools (web status, mail notifier) to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster, mail_notifier=False, order_console_by_time=True,
    public_html='./public_html',
    templates=['./templates', '../master.chromium/templates']
)

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['buildbotURL'] = ActiveMaster.buildbot_url
